import { fileURLToPath, URL } from 'node:url'
// --
import { loadEnv } from "vite";
const root = process.cwd();
// --
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig((config) => {
  let command = config.command;
  let mode = config.mode;
  let env;
  const isBuild = command === "build";
  if (!isBuild) {
    env = loadEnv(
      process.argv[3] === "--mode" ? process.argv[4] : process.argv[3],
      root
    );
  } else {
    env = loadEnv(mode, root);
  }
  return {
    base: "/bdzy",
    plugins: [
      vue(),
      require('postcss-pxtorem')({
        rootValue: 16,
        propList: ['*'],
      })
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    build: {
      minify: "terser",
      outDir: env.VITE_OUT_DIR || "dist",
      sourcemap: env.VITE_SOURCEMAP === "true" ? "inline" : false,
      terserOptions: {
        compress: {
          drop_debugger: env.VITE_DROP_DEBUGGER === "true",
          drop_console: env.VITE_DROP_CONSOLE === "true",
        },
      },
    },
    server: {
      host: '0.0.0.0', //ip地址
      port: 8086, //端口号
      open: false, //启动后是否自动打开浏览器
      proxy: {
        '/api': { // 请求接口中要替换的标识
          // target: 'https://qilihai.baohuqu.net:8080/', // 代理地址
          target: 'http://192.168.1.254:7130/', // 代理地址
          changeOrigin: true, // 是否允许跨域
          secure: true,
          rewrite: (path) => path.replace(/^\/api/, '') // api标志替换为'' 
        },
        '/apr': { // 请求接口中要替换的标识
          target: 'http://123.150.5.131:8080/', // 代理地址
          changeOrigin: true, // 是否允许跨域
          secure: true,
          rewrite: (path) => path.replace(/^\/apr/, '') // api标志替换为''
        }
      }
    }
  }
})
